package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.logic.walk.Astar.ForbiddenAreaLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.radio.NxRadio;
	import newx.com.control.radio.NxRadioEvent;
	import newx.com.control.text.NxLabel;
	
	public class ForbiddenAreaUI extends BaseUI
	{
		private static var self:ForbiddenAreaUI;
		private var model:ModelLocator;
		private var bind:BindManage;
		private var _isAutoAddAction:int	=	0;
		private var imageArr:Array	=	[];
		private var imageWidth:int	=	26;
		private var imageGrap:int	=	10;
		private var _currentAction:int	=	-1;
		
		public var closeBtn:NxButton;
		public var moneyNum:NxLabel;
		public var actionNum:NxLabel;
		public var addBtn:NxButton;
		public var autoLbl:NxLabel;
		public var autoRadio:NxRadio;
		public var enemyNum:NxLabel;
		public var targetLbl:NxLabel;
		public var targetNum:NxLabel;
		public var imagePoint:NxLabel;
		public var picbg:MovieClip;
		
		public function ForbiddenAreaUI(popupUI:Boolean=true)
		{
			super(popupUI);
		}
		
		public function get isAutoAddAction():int
		{
			return _isAutoAddAction;
		}

		public function get currentAction():int
		{
			return _currentAction;
		}

		public static function getInstance():ForbiddenAreaUI
		{
			if(self==null)
			{
				self	=	new ForbiddenAreaUI();
			}
			return self;
		}
		
		override public function Init():void
		{
			model	=	ModelLocator.getInstance();
			bind	=	BindManage.getInstance();
			bind.BindProp(model.scene.Role,"MagicCoin",this,"changePlayerMagicCoin");
			bind.BindProp(model.scene.Role,"AutoAddForbiddenArea",this,"autoRadioSelect");
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_FORBIDDEN_AREA_INFO),showInfo);
		}
		
		private function showInfo(e:CustomMsgEvent):void
		{
			var arr:Array	=	e.pList;
			
			actionNum.text	=	arr[1]+"/"+arr[0];
			enemyNum.text	=	arr[3]+"/"+arr[2];
			targetNum.text	=	StaticIni.getSwfLangStr(arr[4]);
			
			_currentAction	=	arr[1];
			
			clearImageGroup();
			
			var imageInfo:Array	=	arr.slice(5);
			var length:int	=	imageInfo.length;
			for(var i:int=0;i<length;i++)
			{
				var img:NxClip	=	new NxClip();
				img.name		=	imageInfo[i];
				img.addEventListener(MouseEvent.MOUSE_OVER,onShowTips);
				img.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
				img.source		=	ResPath.PNG +"buff_"+ imageInfo[i] + ".png";
				imageArr.push(img);
			}
			
			iconCompositor(imageArr);
		}
		
		private function iconCompositor(arr:Array):void
		{
			for(var i:int=0;	i<arr.length;	i++)
			{
				var icoImg:NxClip	=	arr[i] as NxClip;
				icoImg.x	= 	imagePoint.x + (imageWidth +	imageGrap) * i;
				icoImg.y	=	imagePoint.y;
				addChild(icoImg);
			}
		}
		
		private function onShowTips(e:MouseEvent):void
		{
			var configID:String		=	e.target.name;
			TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("BufferDes_" + configID));
		}
		
		private function onHideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		private function clearImageGroup():void
		{
			while(imageArr.length!=0)
			{
				var img:NxClip	=	imageArr.pop() as NxClip;
				img.removeEventListener(MouseEvent.MOUSE_OVER,onShowTips);
				img.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
				img.source		=	"";
				removeChild(img);
				img				=	null;
			}
		}
		
		override protected function BeforeShow():void
		{
			addEvents();
		}
		
		override protected function AfterHide():void
		{
			removeEvents();
		}
		
		private function addEvents():void
		{
			addBtn.addEventListener(MouseEvent.CLICK,onAddActionBtn);
			addBtn.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			addBtn.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			autoRadio.addEventListener(NxRadioEvent.RADIO_CLICK,onAutoClick);
			closeBtn.addEventListener(MouseEvent.CLICK,onSceneOut);
			closeBtn.visible	=	closeBtn.mouseEnabled	=	closeBtn.mouseChildren	=	false;
		}
		
		private function removeEvents():void
		{
			addBtn.removeEventListener(MouseEvent.CLICK,onAddActionBtn);
			addBtn.removeEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			addBtn.removeEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			autoRadio.removeEventListener(NxRadioEvent.RADIO_CLICK,onAutoClick);
			closeBtn.removeEventListener(MouseEvent.CLICK,onSceneOut);
		}
		
		private function onSceneOut(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LEAVE_FORBID_AREA);
			ForbiddenAreaLogic.getInstance().stopSearch();
		}
		
		private function onAddActionBtn(e:MouseEvent):void
		{
			AlertUI.showAlert("提示",StaticIni.getSwfLangStr("ForbiddenAreaStr_13"),AlertUI.YES|AlertUI.NO,function(type:int, str:String, clickData:*):void
			{
				if (type	==	AlertUI.YES)
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADD_HARDINESS);
				}
			});
		}
		
		private function onMouseOver(e:MouseEvent):void
		{
			TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("ForbiddenAreaStr_15"));
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		private function onAutoClick(e:NxRadioEvent):void
		{
//			_isAutoAddAction	=	_isAutoAddAction==0	?	1	:	0;
			var isAuto		:int=	_isAutoAddAction==0	?	1	:	0;
			if(isAuto==1)
			{
				AlertUI.showAlert("提示",StaticIni.getSwfLangStr("ForbiddenAreaStr_14"),AlertUI.YES|AlertUI.NO,function(type:int, str:String, clickData:*):void
				{
					if (type	==	AlertUI.YES)
					{
						SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADD_OPTION,isAuto);
						(e.currentTarget as NxRadio).selected	=	true;
						_isAutoAddAction	=	1;
					}
					else
					{
						_isAutoAddAction	=	0;
						(e.currentTarget as NxRadio).selected	=	false;
					}
				});
			}
			else
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADD_OPTION,isAuto);
			}
		}
		
		public function set changePlayerMagicCoin( value : int ):void
		{
			moneyNum.text = value.toString();
		}
		
		public function set autoRadioSelect(val:int):void
		{
			_isAutoAddAction		=	val;
			autoRadio.selected	=	val==1?true:false;
		}
		
		
	}
}